<?php
/** @noinspection ALL */
declare (strict_types = 1);

namespace app\patient\controller;
use app\Db2;
use app\AppAuthController;
use think\facade\Db;

class User extends AppAuthController{
    /**
     * @Notes: 编辑个人信息1
     * @Interface saveUser
     * @return mixed
     * @author: Shaco
     * @Time: 2022/8/24-22:45
     */
    public function saveUser(){
        $user = $this->user;
        $id = $user['id'];

        if(!$id || $id == 'undefined') return rejson(302,'必填字段缺失');

        $name = input('name');
        $tel = input('tel');
        $age = input('age');
        $sex = input('sex');
        $high = input('height');
        $weight = input('weight');
        $addressValue = input('addressValue');
        $addressInfo = input('addressInfo');
        $code = input('code');
        $medical_history = input('medical_history');

        if((!$name || $name == 'undefined')
            || (!$tel || $tel == 'undefined')
            || (!$age || $age == 'undefined')
            || (!$sex || $sex == 'undefined')
            || (!$high || $high == 'undefined')
            || (!$weight || $weight == 'undefined')
            || (!$addressValue || $addressValue == 'undefined')
            || (!$addressInfo || $addressInfo == 'undefined')
            || (!$code || $code == 'undefined')
        ) {
            return rejson(302,'必填字段缺失');
        }

        $addressValue = explode(',',$addressValue);
        $province = $addressValue[0];
        $city = $addressValue[1];
        $area = $addressValue[2];

        $data = [
            'name' => $name,
            'tel' => $tel,
            'age' => $age,
            'sex' => $sex,
            'high' => $high,
            'weight' => $weight,
            'code' => $code,
            'province' => $province,
            'city' => $city,
            'area' => $area,
            'address' => $addressInfo
        ];

        if($medical_history){
            $data['medical_history'] = $medical_history;
        }

        $num = Db2::update_data('user',[['id' => $id]],$data);

        $user = Db::name('user')->where(['id' => $id,'isdel' => 0])->find();

        return rejson(200,'保存成功',$user);
    }

    /**
     * @Notes:
     * @Interface hasMustInfo
     * @author: Shaco
     * @Time: 2022/9/3-16:32
     */
    public function hasMustInfo(){
        $user = $this->user;

        if(!$user['name']
            || !$user['tel']
            || !$user['age']
            || !$user['sex']
            || !$user['high']
            || !$user['weight']
            || !$user['code']
            || !$user['province']
            || !$user['city']
            || !$user['area']
            || !$user['address']
        ) return rejson(200, '查询成功', ['has' => false]);

        return rejson(200,'查询成功', ['has' => true]);
    }

    /**
     * @Notes: 获取省市区
     * @Interface getAddress
     * @author: Shaco
     * @Time: 2022/9/28-21:25
     */
    public function getAddress(){
        $data = Db2::get_datalist('address');
        $data = array_to_tree($data,'id','parent_id','children', 1);
        return rejson(200, '操作成功', $data);
    }

    /**
     * @Notes: 获取个人信息
     * @Interface getUserInfo
     * @author: Shaco
     * @Time: 2022/10/11-12:18
     */
    public function getUserInfo(){
        $user = $this->user;

        return rejson(200,'操作成功',$user);
    }

}

